//
//  MainWeb.m
//  JTGJ
//
//  Created by zuqiu on 17/6/27.
//  Copyright © 2017年 zuqiu. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MainWeb.h"
#import "Manger.h"
#import <CoreLocation/CoreLocation.h>
@interface MainWeb ()
<
CLLocationManagerDelegate
>
{
 float latitude; //纬度
    
 float longitude; // 经度
}
@property(nonatomic,strong)CLLocationManager *locMgr;
@property(nonatomic,copy)  NSString * BaiDu_adress;;

@end

@implementation MainWeb

- (void)viewDidLoad {
    [super viewDidLoad];
    self.web.backgroundColor = [UIColor whiteColor];
    [self webProsgress];
}


#pragma mark - 计算wkWebView进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == self.web && [keyPath isEqualToString:@"estimatedProgress"]) {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1) {
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
        }else {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}

// 记得取消监听
- (void)dealloc {
    //这里需要注意，前面增加过的方法一定要remove掉。
    [self killFunc:@"callPhone"];
}


//初始化页面
- (void)initWKWebView:(NSString *)url
{
    
    self.web.backgroundColor = [UIColor whiteColor];
    if (!self.web) {
        self.web = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)  configuration:[self webConfig]];
    }
    self.web.scrollView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    //    self.web.scrollView.contentInset =  UIEdgeInsetsMake(-20, 0, 0, 0);
    //    [self.web addObserver:self forKeyPath:@"scrollView.contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"DJWebKitContext"];
    
    
    //    NSLog(@"%@",ceshiBaidu);shouYePage
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
    //NSLog(@"viewframe页面加载-->%@",NSStringFromCGRect(self.web.frame));
    [self.web loadRequest:request];
    [self.web sizeToFit];
    // 禁止回弹
    self.web.scrollView.directionalLockEnabled = NO;
    self.web.scrollView.bounces = NO;
    self.web.UIDelegate = self;
    self.web.navigationDelegate = self;
    [self.view addSubview:self.web];
}


- (WKWebViewConfiguration *)webConfig
{ //进行配置控制器
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    //实例化对象
    configuration.userContentController = [WKUserContentController new];
    //    调用JS方法
    [configuration.userContentController addScriptMessageHandler:self name:@"callPhone"];

    [configuration.userContentController addScriptMessageHandler:self name:@"AppExit"];
    [configuration.userContentController addScriptMessageHandler:self name:@"setUserid"];

    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    configuration.preferences = preferences;
    return configuration;
}


#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    HJLog(@"body:%@",message.name);
    // LPLog(@"=====>>>%@",a);
    if ([message.name isEqualToString:@"setUserid"]) {
         NSString *tokenStr   = [message.body  objectForKey:@"token"];
        Manger *manger = [Manger shardeManger];
        manger.TOKEN = tokenStr;
        HJLog(@"%@", manger.TOKEN);
    }else  if ([message.name isEqualToString:@"callPhone"]) {
        NSString *tokenStr   = [message.body  objectForKey:@"phoneNumber"];
        [self callPhoneMethod:tokenStr];
    }else  if ([message.name isEqualToString:@"AppExit"]) {
             [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - 退出
- (void)exitWithLeftTable
{
    // 退出
    NSString  *title = [NSString stringWithFormat:@"是否退出"];
    
    //
    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:@"温馨提示"
                                 message:title
                                 preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* yesButton = [UIAlertAction
                                actionWithTitle:@"确定"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        [self.navigationController popViewControllerAnimated:YES];

                                    });
                                }];
    UIAlertAction* noButton = [UIAlertAction
                               actionWithTitle:@"不，谢谢"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                           
                               }];
    [alert addAction:yesButton];
    [alert addAction:noButton];
    [self presentViewController:alert animated:YES completion:nil];
    
}

/**
 *  退出系统
 */
-(void) AppExit {
    
    [self.navigationController popViewControllerAnimated:YES];
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(window.bounds.size.width/2, window.bounds.size.width/2, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
    
}




@end
